<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" clearable placeholder="参数名" />
      </el-form-item>
      <el-form-item>
        <el-button v-has-permission="['member:member:list']" @click="getDataList()">查询</el-button>
        <el-button v-has-permission="['member:member:add']" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-has-permission="['member:member:delete']" type="danger" :disabled="dataListSelections.length <= 0" @click="deleteHandle()">批量删除</el-button>
        <el-button v-has-permission="['member:member:export']" type="info" @click="handleExport()">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column align="center" header-align="center" type="selection" width="50" />
      <el-table-column align="center" header-align="center" label="id" prop="id" />
      <el-table-column align="center" header-align="center" label="代理账户" prop="realName" />
      <el-table-column ref="table" prop="avatar" label="代理头像" align="center">
        <template slot-scope="scope">
          <el-image :src="scope.row.avatar" class="pic-image" :preview-src-list="[scope.row.avatar]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" header-align="center" label="代理昵称" prop="nickname" />-->
      <el-table-column align="center" header-align="center" label="代理手机号" prop="mobile" />
      <el-table-column align="center" header-align="center" label="代理等级" prop="memberLevel.name" />
      <el-table-column align="center" header-align="center" label="邀请人" prop="parentName" />
      <!--      <el-table-column align="center" header-align="center" label="进货上级" prop="nickname" />-->
      <el-table-column align="center" header-align="center" label="数据" width="200">
        <template slot-scope="scope">
          <span style="color: #dd1100">货款余额: {{ scope.row.balance }}</span><br>
          <span style="color: #dd1100">订单业绩: {{ scope.row.orderMoney }}</span><br>
          <span style="color: #dd1100">充值业绩: {{ scope.row.recharge }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="积分" prop="integration" />
      <el-table-column align="center" header-align="center" label="成为代理时间" prop="createTime" />
      <el-table-column v-if="checkPermission(['member:member:editMemberStatus'])" align="center" header-align="center" label="启用状态" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['member:member:edit', 'member:member:delete', 'member:member:editMemberLevel', 'member:member:editMemberParent'])" align="center" fixed="right" header-align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-dropdown size="mini" split-button type="primary" trigger="click">
            操作
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button v-if="checkPermission(['member:member:edit'])" size="small" style="width: 100%; margin: 5px 0" type="danger" plain @click="addOrUpdateHandle(scope.row.id)">编辑代理</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-if="checkPermission(['member:member:edit'])" size="small" style="width: 100%; margin: 5px 0" type="info" plain @click="operationHandler(scope.row, 3)">调整余额</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-if="checkPermission(['member:member:edit'])" size="small" style="width: 100%; margin: 5px 0" type="primary" plain @click="operationHandler(scope.row, 4)">调整积分</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-if="checkPermission(['member:member:editMemberLevel'])" size="small" style="width: 100%; margin: 5px 0" type="success" plain @click="operationHandler(scope.row, 1)">变更等级</el-button>
              </el-dropdown-item>
              <!--              <el-dropdown-item>-->
              <!--                <el-button size="small" style="width: 100%; margin: 5px 0" type="info" plain>变更进货上级</el-button>-->
              <!--              </el-dropdown-item>-->
              <el-dropdown-item>
                <el-button v-if="checkPermission(['member:member:editMemberParent'])" size="small" style="width: 100%; margin: 5px 0" type="warning" plain @click="operationHandler(scope.row, 2)">变更邀请人</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-if="checkPermission(['member:member:delete'])" size="small" style="width: 100%; margin: 5px 0" type="danger" icon="el-icon-delete" plain @click="deleteHandle()">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 10px"
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" />

    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="50%" append-to-body>
      <el-form ref="operationDataForm" :model="operationDataForm" label-width="100px">
        <template v-if="dialog.type === 1">
          <el-form-item label="代理等级">
            <el-select v-model="operationDataForm.levelId" placeholder="请选择代理等级">
              <el-option
                v-for="item in memberLevelList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>
        <template v-if="dialog.type === 2">
          <el-form-item label="代理邀请人">
            <el-select v-model="operationDataForm.parentId" filterable clearable placeholder="请选择邀请人">
              <el-option
                v-for="item in memberList"
                :key="item.id"
                :label="item.realName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>
        <template v-if="dialog.type === 3">
          <el-form-item label="余额">
            <el-input v-model="operationDataForm.balance" disabled />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="operationDataForm.type">
              <el-radio :label="1">增加</el-radio>
              <el-radio :label="0">减少</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="调整金额" prop="editBalance">
            <el-input-number v-model="operationDataForm.editBalance" :min="0" :precision="2" />
          </el-form-item>
        </template>
        <template v-if="dialog.type === 4">
          <el-form-item label="积分">
            <el-input v-model="operationDataForm.integration" disabled />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="operationDataForm.type">
              <el-radio :label="1">增加</el-radio>
              <el-radio :label="0">减少</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="调整金额" prop="editIntegration">
            <el-input-number v-model="operationDataForm.editIntegration" :min="0" :precision="0" />
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOperation">确 定</el-button>
        <el-button @click="cancelDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from './member-add-or-update'
import {
  delMember,
  listMember,
  exportMember,
  editMember,
  editMemberLevel,
  editMemberParent,
  editMemberStatus
} from '@/api/ums/member'
import { listMember_level } from '@/api/ums/member_level'
import { downloadFile } from '@/utils'
import checkPermission from '@/utils/permission'

export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      dataForm: {
        key: ''
      },
      operationDataForm: {
        id: undefined,
        levelId: 0,
        parentId: 0,
        balance: 0,
        integration: 0,
        type: 1,
        editBalance: 0,
        editIntegration: 0
      },
      memberList: [],
      memberLevelList: [],
      dialog: {
        title: undefined,
        visible: false,
        type: undefined // type 操作类型：1-新增 2-修改
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    checkPermission,
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      listMember({
        pageNum: this.pageIndex,
        pageSize: this.pageSize,
        key: this.dataForm.key
      }).then(({ data }) => {
        this.dataList = data.list
        this.totalPage = data.totalCount
        this.dataListLoading = false
      })
    },
    getAllMemberList() {
      listMember({
        queryMode: 'list',
        levelId: '5'
      }).then(({ data }) => {
        this.memberList = data
      })
    },
    getMemberLevelList() {
      listMember_level({ queryMode: 'list', status: 1 }).then(res => {
        this.memberLevelList = res.data
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    handleStatusChange(val) {
      editMemberStatus({ id: val.id, status: val.status }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功',
          duration: 1500,
          onClose: () => {
            this.getDataList()
          }
        })
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出所有数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportMember()
      }).then(response => {
        downloadFile(response.data, 'client', 'xls')
      })
    },
    // 删除
    deleteHandle(id) {
      const ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delMember(ids).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      })
    },
    operationHandler(val, type) {
      this.operationDataForm.id = val.id
      let title = ''
      if (type === 1) {
        title = '变更代理等级'
        this.getMemberLevelList()
        this.operationDataForm.levelId = val.levelId
      } else if (type === 2) {
        title = '变更代理邀请人'
        this.getAllMemberList()
        this.operationDataForm.parentId = val.parentId
      } else if (type === 3) {
        title = '调整代理余额'
        this.operationDataForm.balance = val.balance
      } else if (type === 4) {
        title = '调整代理积分'
        this.operationDataForm.integration = val.integration
      }
      this.dialog = {
        title: title,
        visible: true,
        type: type
      }
    },
    submitOperation() {
      if (this.dialog.type === 1) {
        editMemberLevel({ id: this.operationDataForm.id, levelId: this.operationDataForm.levelId }).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功',
            duration: 1500,
            onClose: () => {
              this.cancelDialog()
              this.getDataList()
            }
          })
        })
      }
      if (this.dialog.type === 2) {
        editMemberParent({ id: this.operationDataForm.id, parentId: this.operationDataForm.parentId }).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功',
            duration: 1500,
            onClose: () => {
              this.cancelDialog()
              this.getDataList()
            }
          })
        })
      }
      if (this.dialog.type === 3 || this.dialog.type === 4) {
        editMember(this.operationDataForm).then(res => {
          this.$message({
            message: res.message,
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.cancelDialog()
              this.getDataList()
            }
          })
        })
      }
    },
    cancelDialog() {
      this.operationDataForm = {
        id: undefined,
        levelId: 0,
        parentId: 0,
        balance: 0,
        integration: 0,
        type: 1,
        editBalance: 0,
        editIntegration: 0
      }
      this.closeDialog()
    },
    closeDialog() {
      this.dialog = {
        title: undefined,
        visible: false,
        type: undefined
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.pic-image {
  width: 40px;
  height: 40px;
  border-radius: 100%;
}
</style>
